package com.jeady.timetodo.activities.lock

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.jeady.timetodo.activities.common.CommonTaskUI
import com.jeady.timetodo.activities.common.TaskAction
import com.jeady.timetodo.activities.task.TaskType


class LockActivity : ComponentActivity() {
    private val TAG = "[LockActivity]"
    private lateinit var launcherGrantLock: ActivityResultLauncher<Intent>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val screenController = ScreenController.getInstance(this)
        launcherGrantLock = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result->
            ScreenController.hasLockPermission.value = result.resultCode == RESULT_OK
        }
        setContent {
            val context = LocalContext.current
            CommonTaskUI(
                "定时自动锁屏",
                "使用此功能前请确保记得手机密码\n一切后果自行承担。",
                listOf(
                    TaskAction("自动锁屏", TaskType.LOCK_SCREEN),
                ), ScreenController.hasLockPermission.value
            ){
                TextButton(modifier = Modifier
                    .padding(10.dp)
                    .align(Alignment.CenterHorizontally)
                    .border(
                        BorderStroke(1.dp, Color.Gray), RoundedCornerShape(10.dp)
                    ),
                    onClick = {
                        screenController?.enablePhone(context, launcherGrantLock)
                    }
                ) {
                    Text(
                        "${if(ScreenController.hasLockPermission.value) "取消" else ""}授权锁屏功能",
                        color=if(!ScreenController.hasLockPermission.value) Color.Red else Color.Black
                    )
                }
            }
        }
    }
}
